<?php

/**
 * This is the model class for table "{{questionario}}".
 *
 * The followings are the available columns in table '{{questionario}}':
 * @property integer $id_questionario
 * @property double $versao
 * @property string $nome
 * @property string $descricao
 * @property integer $situacao
 *
 * The followings are the available model relations:
 * @property Questao[] $questaos
 * @property Eixo[] $tbEixos
 */
class Questionario extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Questionario the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{questionario}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('versao, nome, descricao, situacao', 'required'),
			array('situacao', 'numerical', 'integerOnly'=>true),
			array('versao', 'numerical'),
			array('nome', 'length', 'max'=>45),
			array('descricao', 'length', 'max'=>200),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_questionario, versao, nome, descricao, situacao', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                        'questoes' => array(self::HAS_MANY, 'Questao', 'id_questionario'),
			'eixos' => array(self::MANY_MANY, 'Eixo', '{{questionario_eixo}}(id_questionario, id_eixo)'),
                        'resultados' => array(self::HAS_MANY, 'Resultado', 'id_resultado'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_questionario' => 'Id Questionário',
			'versao' => 'Versão',
			'nome' => 'Nome',
			'descricao' => 'Descrição',
			'situacao' => 'Situação',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_questionario',$this->id_questionario);
		$criteria->compare('versao',$this->versao);
		$criteria->compare('nome',$this->nome,true);
		$criteria->compare('descricao',$this->descricao,true);
		$criteria->compare('situacao',$this->situacao);
                
                $criteria->condition = 'situacao <> 99';

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        public function getSituacao()
        {
            return $this->situacao == 0 ? 'Inativo' : 'Ativo';
        }

        public function hasEixos($q)
        {
            $busca = QuestionarioEixo::model()->findAll('id_questionario=:id_questionario',array(':id_questionario'=>$q));
            
            if(sizeof($busca) > 0)
                return true;
            else
                return false;
        }
        
        public function hasQuestoes($q)
        {
            $busca = Questao::model()->findAll('id_questionario = :id_questionario',array(':id_questionario'=>$q));
            
            if(sizeof($busca) > 0)
                return true;
            else
                return false;
        }  
        
        public function hasResultados($q)
        {
            $busca = Resultado::model()->findAll('id_questionario = :id_questionario',array(':id_questionario'=>$q));
            
            if(sizeof($busca) > 0)
                return true;
            else
                return false;
        }

        public function jaRespondeu($q,$u)
        {
            $busca = Resultado::model()->findAll('id_questionario = :id_questionario AND id_usuario = :id_usuario',array(':id_questionario'=>$q,':id_usuario'=>$u));
            
            if(sizeof($busca) > 0)
                return true;
            else
                return false;
        }
        
        public function calcMax()
        {
            $eixosQuestionario = QuestionarioEixo::model()->findAll('id_questionario = :id_questionario',array(':id_questionario'=>$this->id_questionario));
            $questoes = Questao::model()->with('maxFM')->findAll('id_questionario = :id_questionario',array(':id_questionario'=>$this->id_questionario));
            
            foreach($eixosQuestionario as $i=>$eixoQuestionario)
            {
                $resultado[$i] = 0;
            }
            
            foreach($questoes as $questao)
            {
                $maxFM = $questao->maxFM;
                $eixosQuestao = EixoQuestao::model()->findAll('id_questao = :id_questao',array(':id_questao'=>$questao->id_questao));
                
                foreach($eixosQuestao as $i=>$eixoQuestao)
                {
                    $resultado[$i] += $maxFM * $eixoQuestao->peso;
                }
            }
            
            return $resultado;
        }
        
        public function calcMin()
        {
            $eixosQuestionario = QuestionarioEixo::model()->findAll('id_questionario = :id_questionario',array(':id_questionario'=>$this->id_questionario));
            $questoes = Questao::model()->with('minFM')->findAll('id_questionario = :id_questionario',array(':id_questionario'=>$this->id_questionario));
            
            foreach($eixosQuestionario as $i=>$eixoQuestionario)
            {
                $resultado[$i] = 0;
            }
            
            foreach($questoes as $questao)
            {
                $minFM = $questao->minFM;
                $eixosQuestao = EixoQuestao::model()->findAll('id_questao = :id_questao',array(':id_questao'=>$questao->id_questao));
                
                foreach($eixosQuestao as $i=>$eixoQuestao)
                {
                    $resultado[$i] += $minFM * $eixoQuestao->peso;
                }
            }
            
            return $resultado;            
        }
         
}